/**
 * 折线图数据处理
 *
 * @param {data} array 数据
 * @return {options} {返回配置}
 */

const linePosLeft = 20;
const linePosTop = 240;
const defaultSelected = 5;
export const COLORS = ['#FF6C00', '#FDCD38', '#289AAD'];

export const lineOptions = (data, period) => {
    data.sort((a, b) => parseFloat(b.point[0].value) - parseFloat(a.point[0].value));
    let legendData = [];
    let xAxisData = [];
    const series = [];
    // 处理图列
    legendData = data.map(e => {
        if (e.name) {
            const Index = e.name.indexOf('</a>');
            if (Index > -1) {
                const startA = e.name.indexOf('<a');
                const startIndex = e.name.indexOf('>');
                const endIndex = e.name.indexOf('</a>');
                const value = e.name.slice(startIndex + 1, endIndex);
                e.name = e.name.slice(0, startA) + value + e.name.slice(endIndex + 4);
                return e.name;
            }
        }
        return e.name;
    });
    legendData = legendData.filter(item => item !== '');
    // 计算横坐标，获取长度最长的
    const lenArr = data.map(item => item.point.length);
    const maxIndex = lenArr.indexOf(Math.max(...lenArr));
    if (maxIndex > -1) {
        xAxisData = data[maxIndex].point.map(e => {
            if (e.xAixs) {
                const Index = e.xAixs.indexOf('</a>');
                if (Index > -1) {
                    const titleIndex = e.xAixs.indexOf('>');
                    e.xAixs = e.xAixs.slice(titleIndex + 1, Index);
                }
            }
            return e.xAixs;
        });
    }

    // 处理x轴数据series： { name: '', type: 'line', smooth: true, data: [] }
    const seriesItem = {type: 'line'};
    for (let i = 0; i < data.length; i++) {
        const item = {...seriesItem};
        item.name = data[i].name;
        item.data = [];
        for (let j = 0; j < xAxisData.length; j++) {
            data[i].point = data[i].point.map(e => {
                if (e.xAixs) {
                    const Index = e.xAixs.indexOf('</a>');
                    if (Index > -1) {
                        const titleIndex = e.xAixs.indexOf('>');
                        e.xAixs = e.xAixs.slice(titleIndex + 1, Index);
                    }
                }
                return e;
            });
            const dataIndex = data[i].point.findIndex(item => item.xAixs === xAxisData[j]);
            if (dataIndex > -1) {
                item.data[j] = parseFloat(data[i].point[dataIndex].value);
            }
            else {
                item.data[j] = 0;
            }
        }
        series.push(item);
    }
    series.map(item => {
        item.data.reverse();
        return item;
    });
    xAxisData = xAxisData.reverse();
    xAxisData = xAxisData.map(item => {
        if (item && period === '小时') {
            item = `${item.slice(4, 6)}-${item.slice(6, 8)} ${item.slice(8, 10)}:00`;
        }
        return item;
    });
    const selectData = data.map(({...params}) => ({...params}));
    const selected = {};
    const defalutShowLegend = selectData.slice(defaultSelected);
    defalutShowLegend.forEach(item => {
        const Index = item.name.indexOf('</a>');
        if (Index > -1) {
            const titleIndex = item.name.indexOf('>');
            item.name = item.name.slice(titleIndex + 1, Index);
        }
        selected[item.name] = false;
    });
    const option = {
        grid: {left: 35, right: 40, bottom: 70, top: 30, containLabel: true},
        legend: {
            type: 'scroll',
            left: 'center',
            bottom: 40,
            selectedMode: 'multiple',
            selected,
        },
        tooltip: {
            trigger: 'axis',
            // position: [10, 10],
            extraCssText: 'max-height: 300px; overflow-y: scroll;'
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: [],
            axisLabel: {textStyle: {fontSize: 9}},
            axisPointer: {
                snap: true,
                handle: {
                    show: true,
                    size: [0, 0]
                }
            },
        },
        yAxis: {
            type: 'value', splitNumber: 10, splitLine: {show: true},
            axisLabel: {textStyle: {fontSize: 10}}
        },
        series: []
    };
    option.legend.data = legendData;
    option.xAxis.data = xAxisData;
    option.series = series;
    return {...option};
};
